// pages/tabBar/index/index.js
import tui from './../../../common/httpRequest'
let dateTime;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskbg: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/task_bg.jpg',
    list: [
      
    ],
    name: '全员',
    nameVal: '1',
    show__1: false,
    items__1: [
      {
        text: "全员",
        value: "1"
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    var myDate = new Date();//获取系统当前时间
    let year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    let month = myDate.getMonth() + 1;
   
    // if(name!=undefined){
    //   this.setData({
    //     name:name==''?'全员':name
    //   })
    //   // getApp().globalData.searchName;/
    //   this.getList()
    // }else{
    //   this.setData({
    //     name:'全员'
    //   })
    // }

    
    this.getUser()
    this.setData({
      result: year + '-' + month,
      year: year,
      month: month,
      storgeName:wx.getStorageSync('name')
    })
    this.getHeight()
    // this.getList()

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    dateTime = this.selectComponent("#dateTime")

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let name= wx.getStorageSync('searchName');
    if(getApp().globalData.ifHide=='2'){
      this.setData({
        name:name==''?'全员':name
      })
      this.getList()
    }else{
      // this.setData({
      //   name:name==''?'全员':name
      // })
    }
    if(getApp().globalData.ifEdit=='1'){
      this.getList()
      getApp().globalData.ifEdit='2'
    }else{

    }
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        current: '1',
        ['tabBar[2].num']:getApp().globalData.ifExemine
      })
    }
   
  },
  getHeight() {
    const capsuleInfo = wx.getMenuButtonBoundingClientRect();//胶囊信息
    const systemInfo = wx.getSystemInfoSync(); // 获取设备信息
    const statusHeight = systemInfo.statusBarHeight // 状态栏高度
    const clientHeight = systemInfo.windowHeight, clientWidth = systemInfo.windowWidth, rpxR = 750 / clientWidth;
    const calc = clientHeight * rpxR;
    this.setData({
      winHeight: calc,
      height: capsuleInfo.height,
      statusHeight: statusHeight,
      top: (capsuleInfo.top + statusHeight) / 2,
      topD: 45 + statusHeight,
      heightList: clientHeight - 154 - 50 - 40-40
    })
    // console.log(capsuleInfo.top, statusHeight)
  },
  async getUser() {
    this.setData({
      items__1: []
    })
    const res = await tui.request('sys/user/queryUserList', 'GET', {
      departNum:wx.getStorageSync('department'),
    }, true);
    if (res.code == 20000) {
      let arr = this.data.items__1
      res.data.forEach(element => {
        arr.push({
          text: element.fullname,
          value: element.id
        })
      });
      this.setData({
        items__1: arr
      })
    }
  },
  async getList() {
    const res = await tui.request('sys/task/list', 'GET', {
      name: this.data.name == '全员' ? '' : this.data.name,
      year: this.data.year,
      month: this.data.month,
      departNum:wx.getStorageSync('department'),
    }, true);
    this.setData({
      list: []
    })
    if (res.code == 20000) {
      let list = res.data
      // list.forEach(element => {
      //   element.taskScore = Number(element.taskScore).toFixed(2)
      // });
      

      this.setData({
        list: list
      })
    }
  },
  openPerson() {
    this.setData({
      show__1: true
    })
  },
  changePerson(e) {
    this.setData({
      name: e.detail.text,
    })
    this.getList()
  },
  hidePerson(e) {
    this.setData({
      show__1: false
    })
  },
  // 时间选择
  change: function (e) {
    this.setData({
      result: e.detail.result,
      year: e.detail.year,
      month: e.detail.month,
    })
    this.getList()
  },
  show: function (e) {
    dateTime && dateTime.show();
  },
  toAdd() {
    wx.navigateTo({
      url: '/packages/task/taskAdd/index',
    })
    getApp().globalData.ifHide='1'
  },
  toPage(e) {
    // type8  是进行中,非本人账号状态
    if(e.target.dataset.type == 3){
    wx.navigateTo({
      url: '/packages/task/taskEdit/index?id=' + e.target.dataset.id,
    })
    getApp().globalData.ifHide='1'

    }else{
      // 详情
      wx.navigateTo({
        url: '/packages/task/detail/index?id=' + e.target.dataset.id,
      })
      getApp().globalData.ifHide='1'
    }
    
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
   
    if(getApp().globalData.ifHide=='2'){
      // 刷新  更新进度
      getApp().globalData.searchName=''
      this.setData({
        name:'全员'
      })
      this.getList()
    }else{
      // 不刷新   详情、新增
    }
    dateTime && dateTime.hide();
    this.setData({
      show__1:false
    })
    getApp().globalData.ifEdit='2'
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})